package unit;

import hospital.Hospital;
import emergency.Fire;

/**
 * A class to create the specific unit FireTruck
 */
public class FireTruck extends Unit {

	/**
	 * Variable registering the maximum size of fire this firetruck can handle
	 */
	private Fire.Size maxFireSize;

	/**
	 * Initialize this new firetruck with a given
	 * name,speed,garage(homelocation) and maximum firesize
	 * 
	 * @param name
	 *            The name for this firetruck
	 * 
	 * @param location
	 *            The location for this firetruck
	 * 
	 * @param garage
	 *            The garage where this firetruck has to be added to
	 * 
	 * @param maxFireSize
	 *            The maximum size of fire this firetruck can handle
	 * 
	 * @post The name of this new firetruck is the same as the given name.
	 * 
	 * @post The location of this new firetruck is the same as the given
	 *       location.
	 * 
	 * @post This firetruck is added to the given garage
	 * 
	 * @post The maximum fire size of this FireTruck is set
	 * 
	 * */
	public FireTruck(String name, int speed, Garage garage,
			Fire.Size maxFireSize) {
		super(name, speed, garage);
		setMaxFireSize(maxFireSize);
	}

	/**
	 * Assign the hospital where to this fireTruck has to drive
	 * 
	 * @throws IllegalArgumentException
	 *             You can't assign a hospital to a fireTruck
	 */
	@Override
	public void assignHospital(Hospital h) {
		throw new IllegalArgumentException(
				"The chosen unit isn't an ambulance!");
	}

	/**
	 * Getters for getting the maximum size of fire this FireTruck can handle
	 * 
	 * @return Returns the maximum size of fire this FireTruck can handle
	 */
	public Fire.Size getMaxFireSize() {
		return maxFireSize;
	}

	/**
	 * Method for revoking this fireTruck
	 * 
	 * @throws IllegalAccessException
	 *             , a firetruck can't be revoked!
	 */
	@Override
	public void revokeUnit() throws IllegalAccessException {
		throw new IllegalAccessException(this.getName()
				+ " was not revoked: firetruck");
	}

	/**
	 * Setter for setting the maximum fire size this FireTruck can handle
	 * 
	 * @param maxFireSize
	 *            , the maximum size of fire this truck can handle
	 * 
	 * @post The size is set as the given size
	 * 
	 * @throws IllegalArgumentException
	 *             , if maxFireSize is null (not allowed)
	 */
	public void setMaxFireSize(Fire.Size maxFireSize) {
		if (maxFireSize != null)
			this.maxFireSize = maxFireSize;
		else
			throw new IllegalArgumentException(
					"The maximum size of fire cannot be null");
	}

	/**
	 * Method for getting the total description of this FireTruck
	 * 
	 * @return Returns the total description of this FireTruck
	 */
	@Override
	public String showInfo() {
		String output = "Firetruck " + this.getName() + "\n";
		if (this.isAssigned()) {
			output += "current assignment: " + this.getEmergency().toString()
					+ "\n";
			if (this.getCurrentLocation() == this.getEmergency().getLocation()) {
				output += "unit is at emergency\n";
			} else {
				output += "unit isn't at emergency\n";
			}
		}
		output += "unit is waiting for an emergency\n";
		return output;
	}

	/**
	 * Return the type of unit, in this case FireTruck
	 * 
	 * @return Returns a string representation of the type of this unit and it's
	 *         name, in this case FireTruck
	 */
	@Override
	public String toString() {
		return "FireTruck-" + getName();
	}

	/**
	 * Method for checking all the firetrucks are on the emergency ( else they
	 * can't do their task )
	 * 
	 * @return Returns true if all firetrucks are on the emergency, false if not
	 * 
	 * @throws IllegalArgumentException, if this firetruck doesn't has an emergency
	 */
	@Override
	public boolean unitsOfSameTypeOnEmergency() {
		boolean result = true;
		if (getEmergency() != null) {
			for (Unit u : getEmergency().getUnitsClone()) {
				if (u instanceof FireTruck)
					if (!u.isAtEmergency())
						result = false;
			}
		}
		else{
			throw new IllegalArgumentException("This firetruck doesn't has an emergency");
		}
		return result;
	}

}
